package com.software.driver.dao;

import java.security.NoSuchAlgorithmException;
import java.util.List;

public class DriverTransaction {

	private DriverDao driverDao;

	
	public DriverDao getDriverDao() {
		return driverDao;
	}

	public void setDriverDao(DriverDao driverDao) {
		this.driverDao = driverDao;
	}
	
	private String name;

	public boolean login(String email,String password){
		Driver driver = new Driver();
		driver.setEmail(email);
		driver.setPassword(password);
		Driver d = driverDao.queryDriver(driver);
		if(d != null){
			if(password.equals(d.getPassword())){
				name = d.getName();
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 
	 * 前提：判断账户是否已经被注册
	 * @param username
	 * @param password
	 * @param email
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public boolean register(String name,String password, String email,String phone,String moto){
		Driver driver = new Driver();
		driver.setEmail(email);
		driver.setPassword(password);
		driver.setPhone(phone);
		driver.setMoto(moto);
		driver.setName(name);
		
		Driver d = driverDao.queryDriver(driver);
		if(d != null){
			return false;
		}
		try{
			driverDao.addDriver(driver);
			this.name = name;
			return true;
		}catch(Exception e){
			return false;
		}	
	}
	
	public String getName(){
		return this.name;
	}
	
	public List<Driver> getDriversByMoto(String email){
		return driverDao.queryDriverByMoto(email);
	}
}
